﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using ADNLib;

namespace ADNCanvas
{
	//works like ToolClone, but sprays objects along path while mouse down

	public class ToolSpray : Tool
	{
		private bool cursorSet = false;
		private bool cloning = false;
        private XObject cloneObj = null;
        private PointF sourcePoint = PointF.Empty;

		public ToolSpray()
		{
			OnCursorChange(Cursors.Hand);
		}

        public override RectangleF EraseRect
        {
            get { return base.EraseRect; }
        }

		public override void MouseDown(PointF location, System.Windows.Forms.MouseButtons button, int clicks)
		{
			if (cloneObj == null)
			{
				int index = HitTestObjects(location);

				if (index != -1)	//no hit
				{
					cloneObj = xObjects[index];
                    currentShape = (Shape)AnimIO.CloneObject(cloneObj); 
					OnCursorChange(Cursors.Default);
                    sourcePoint = prevPoint = location;
                    cloning = true;
				}
			}
			else
			{
				//create a duplicate object
                OnShapeComplete(true);
				currentShape = (Shape)AnimIO.CloneObject(cloneObj);  //TODO: change to XObject
                prevPoint = sourcePoint;
                PositionClone(location);
				prevPoint = location;
			}
		}

		public override void MouseMove(PointF location, System.Windows.Forms.MouseButtons button, int clicks)
		{
			if (!cursorSet && !cloning) 
            { 
                OnCursorChange(Cursors.Hand);
                cursorSet = true;
            }

			if (cloneObj != null && currentShape != null)
			{
                PositionClone(location);
				prevPoint = location;
			}
		}

		public override void MouseDoubleClick(PointF location, System.Windows.Forms.MouseButtons button, int clicks)
		{
            OnToolComplete(true);
            cloneObj = null;
            cloning = false;
		}

		private void PositionClone(PointF location)
		{
            if (currentShape != null && currentShape.Path.Points.Count > 0)
            {
                PointF p = new PointF(location.X - prevPoint.X, location.Y - prevPoint.Y);
                currentShape.Move(p);
            }
		}
	}
}
